<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class InfoController extends Controller
	{
		/** 
		 *获取才艺标签
		 * 用户id值
		 * 用户手机token值 
		 *  
		 */
		public function taginfo()
		{
			$taginfo = M("taginfo") -> select();
			
			if ($taginfo == null) 
			{
				$array['result'] = -1;
				$array['message'] = "请求数据失败！";
				$array['taginfo'] = $taginfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = 0;
				$array['message'] = "请求成功！";
				$array['taginfo'] = $taginfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}	
		}
		/**
		 *获取身价
		 * 用户id值
		 * 用户手机token值
		 *
		 */
		public function spreadinfo()
		{
			$spreadinfo = M("Spread") -> select();
			if ($spreadinfo == null) 
			{
				$array['result'] = 10311;
				$array['message'] = "请求数据失败！";
				$array['spreadinfo'] = $spreadinfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else 
			{
				$array['result'] = 0;
				$array['message'] = "请求成功！";
				$array['spreadinfo'] = $spreadinfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			
		} 
		
		/**
		 *地址信息
		 * 
		 *
		 */
		public function addressinfo()
		{
			$provinceinfo = M('Province') -> select();
			foreach($provinceinfo as $key=>$value)
			{
				$cityinfo = M("City")->where("fatherid = {$value['provinceid']}")->select();
				$val = array();
				foreach ($cityinfo as $k=>$v)
				{
					
					$val[$k]['cityid'] = $v['cityid'];
					$val[$k]['cityname'] = $v['city'];
					 
				}
				$provinceinfo[$key]['city'] = $val;
			}
			

			if ($provinceinfo == null) 
			{
				$array['result'] = -1;
				$array['message'] = "请求数据失败！";
				$array['addressinfo'] = $provinceinfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = 0;
				$array['message'] = "请求成功！";
				$array['provinceinfo'] = $provinceinfo;  
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			} 
		 }	
		/*
		 *推荐格言 
		 */
 		public function recommand()
 		{
 			//随即选取五条数据返回
 			$sql = "SELECT content FROM __MOTTO__ WHERE mottoid >= (SELECT floor(RAND() * (SELECT MAX(mottoid) FROM __MOTTO__))) ORDER BY mottoid LIMIT 5";
			/* $mottoinfo = M("motto") -> where("content like '%{$motto}%'") -> select(); */
 			$mottoinfo = M("Motto") -> query($sql);
 			if ($mottoinfo != null) 
 			{
 				$array['result'] = 0;
 				$array['message'] = "请求成功！";
 				$array['mottoinfo'] = $mottoinfo;
 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
 				exit;
 			}
 			else
 			{
 				$array['result'] = -1;
				$array['message'] = "请求数据失败！";
 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
 				exit;
			}
 		}
 		/*
		 *推荐昵称
 		 */
 		public function recommandNickname()
 		{
 			//随机选取五个昵称
 			$sql = "SELECT uid,nickname,rmtype FROM __USER__ WHERE type=0 and uid >= (SELECT floor(RAND() * (SELECT MAX(uid) FROM __USER__))) ORDER BY uid LIMIT 5";
 			$nickinfo = M("User") -> query($sql);
			foreach ($nickinfo as $key => $value)
			{
				if ($value['rmtype'] != null) 
				{
					$nickinfo[$key]['headimg'] = M("Userinfo") -> where("uid = {$value['uid']}") -> getField('headimg');
				}else{
					$nickinfo[$key]['headimg'] = null;
				}
				
			}
			if ($nickinfo != null) 
			{
				$array['result'] = 0;
				$array['message'] = "请求成功！";
				$array['mottoinfo'] = $nickinfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "请求数据失败！";
				$array['mottoinfo'] = $nickinfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		/*
		 *获取个人/企业资料 
		 *   
		 *   
		 */
		public function getUserInfo($token=null, $uid=null)
		{
			$userinfo = M("User") -> where("uid={$uid}") -> find();
			/* if ($userinfo['token'] != $token) 
			{
				$array['result'] = -2;
				$array['message'] = "token过期或错误，请重新登录获取";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			} 
			else
			{ */
				if ($userinfo['type'] == 0) 
				{
					if ($userinfo['rmtype'] == 0) 
					{
						$userData = array(
								'headimg'=>"",
								'nickname'=>$userinfo['nickname'],
								'sex'=>"",
								'position'=>"",
								'birthdate'=>"",
								'height'=>"",
								'weight'=>"",
								'sanwei'=>"",
								'tagname'=>array(),
								'interest'=>"",
								'motto'=>""
						
						);
						
// 						$userinfo['info'] = array();
						$array['result'] = 0;
						$array['message'] = "用户信息获取成功！";
						$array['userinfo'] = $userData;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					elseif ($userinfo['rmtype'] == 1)
					{
						$removeinfo = M("Userinfo") -> where("uid={$uid}") -> find();
						if($removeinfo['tagname']!=null)
						{
							$tagname = explode("-", $removeinfo['tagname']);
							unset($removeinfo['tagname']);
							$removeinfo['tagname'] = $tagname;
						}
						else
						{
							$removeinfo['tagname'] = array();
						}
						
						
						/* if($removeinfo['tscontent']!=null)
						$tscontent = explode("-", $removeinfo['tscontent']);
						unset($removeinfo['tscontent']); 
						$removeinfo['tscontent'] = $tscontent;*/
						if ($removeinfo['spreadid'] != null)
						{
							$sprice = M("Spread") -> where("spreadid = {$removeinfo['spreadid']}") -> find();
							$removeinfo['spreadid'] = $sprice;
						}
						if ($removeinfo['mottoid'] != null) 
						{
							$motto = M("Motto") -> where("mottoid = {$removeinfo['mottoid']}") ->find();
							$removeinfo['mottoid'] = $motto['content '];
						}else{
							$removeinfo['mottoid'] = null;
						}
						$userinfo['info'] = $removeinfo;
						
						$userData1 = array(
								'headimg'=>$removeinfo['headimg'],
								'nickname'=>$userinfo['nickname'],
								'sex'=>$removeinfo['sex'],
								'position'=>$removeinfo['position'],
								'birthdate'=>$removeinfo['birthdate'],
								'height'=>$removeinfo['height'],
								'weight'=>$removeinfo['weight'],
								'sanwei'=>$removeinfo['sanwei'],
								'tagname'=>$removeinfo['tagname'],
								'interest'=>$removeinfo['interest'],
								'motto'=>$removeinfo['mottoid']
								
						);
						
						$array['result'] = 0;
						$array['message'] = "用户信息获取成功！";
						$array['userinfo'] = $userData1;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				else
				{
					if ($userinfo['rmtype'] == 0)
					{
						$cpdata = array(
								'headimg'=>"",
								'nickname'=>$userinfo['nickname'],
								'content'=>"",
								'tagname'=>array(),
								'linkman'=>"",
								'address'=>"",
								'email'=>"",
								'phone'=>""
						
						);
						
						$array1['result'] = 0;
						$array1['message'] = "企业信息获取成功！";
						$array1['companyinfo'] = $cpdata;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					elseif ($userinfo['rmtype'] == 1)
					{
						$removeinfo = M("Company") -> field("content,cplogo,tagname,linkman,address,email,phone") -> where("uid={$uid}") -> find();
						if($removeinfo['tagname']!=null)
						{
							$tagname = explode("-", $removeinfo['tagname']);
							unset($removeinfo['tagname']);
							$removeinfo['tagname'] = $tagname;
						}
						else
						{
							$removeinfo['tagname'] = array();
						}
						$userinfo['info'] = $removeinfo;
						
						$cpdata1 = array(
								'headimg'=>$removeinfo['cplogo'],
								'nickname'=>$userinfo['nickname'],
								'content'=>$removeinfo['content'],
								'tagname'=>$removeinfo['tagname'],
								'linkman'=>$removeinfo['linkman'],
								'address'=>$removeinfo['address'],
								'email'=>$removeinfo['email'],
								'phone'=>$removeinfo['phone']
						
						);
						
						$array['result'] = 0;
						$array['message'] = "企业信息获取成功！";
						$array['companyinfo'] = $cpdata1;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
			/* } */ 
		}
// 		/*
// 		 *获取艺人库资料
// 		 *
// 		 *
// 		 */
// 		public function artistInfo($page=null)
// 		{
			
// 		    $artistinfo1 = M("User")-> join("__USERINFO__ on __USER__.uid = __USERINFO__.uid") -> where("rmtype=1") -> select();
// 		    //var_dump($artistinfo1);
// 		    $artistinfo2 = M("User")-> where("rmtype=0") -> select();
			
// 			foreach ($artistinfo1 as $key => $value)
// 			{
// 				$tagid = explode("-", $value['tagid']);
// 				unset($value['tagid']);
// 				$value['tagid'] = $tagid;
// 				if ($value['spreadid'] != null) 
// 				{
// 					$sprice = M("Spread") -> where("spreadid = {$value['spreadid']}") -> find();
// 					$value['spreadid'] = $sprice;
// 				}
				
				
// 				$artistinfo1[$key] = $value;
// 			}
// 			//var_dump($sprice);
// 			/* foreach ($artistinfo as $key => $value)
// 			{
// 				$artist = M("User") -> where($where) -> having() -> select();
// 			} */
// 			foreach ($artistinfo1 as $key => $value)
// 			{
// 				$userlist[$key] = array
// 				(
// 						'userimg'=>$value['headimg'],
// 						'nickname'=>$value['nickname'],
// 						'sex'=>$value['sex'],
// 						'tag'=>$value['tagid'],
// 						'price'=>$sprice['spread'],
// 						'position'=>$value['position']
// 				);
// 			}
			
// 			foreach ($artistinfo2 as $key => $value)
// 			{
// 				$userlist1[$key] = array
// 				(
// 						'userimg'=>$value['headimg'],
// 						'nickname'=>$value['nickname'],
// 						'sex'=>$value['sex'],
// 						'tag'=>$value['tagid'],
// 						'price'=>$value['spreadid'],
// 						'position'=>$value['position']
// 				);
// 			}
// 			array_push($userlist, $userlist1);
// 			//var_dump($userlist);
// 			$returnlist = array();
// 			if(($page)*10>$userlist1){
//  				for ($i = ($page-1)*10; $i<sizeof($userlist1);$i++){
// 					$returnlist[] = $userlist1[$i];
// 				}
// 			}else{
// 				for ($i = ($page-1)*10; $i<($page)*10;$i++){
// 					$returnlist[] = $userlist1[$i];
// 				}
// 			}
// 			var_dump($returnlist);
// 			/* foreach ($i = $Page*10; $i<($page+1)*10;$i++){
				
// 			} */
// 			//var_dump($userlist);
// 			/* if ($userlist == null) 
// 			{
// 				$array['result'] = -1;
// 				$array['message'] = "艺人信息获取失败！";
// 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 				exit;
// 			}
// 			else
// 			{
// 				$array['result'] = 0;
// 				$array['message'] = "艺人信息获取成功！";
// 				$array['userlist'] = $userlist;
// 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 				exit;
// 			}  */
// 			/* $User = M('User'); // 实例化User对象
// 			$count      = count($userlist);// 查询满足要求的总记录数
// 			$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
// 			$show       = $Page->show();// 分页显示输出
// 			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
// 			$list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
// 			/* $this->assign('list',$list);// 赋值数据集
// 			$this->assign('page',$show);// 赋值分页输出
// 			$this->display(); // 输出模板 
// 			var_dump($count);
// 			var_dump($show); */
// 		}
// 		public function arInfo($currentPage=null)
// 		{
// 			$artistinfo = M("User") -> order("dtttype desc,rmtype desc") ->select();
// 			foreach ($artistinfo as $key=>$value)
// 			{
// 				if ($value['rmtype'] == 1) 
// 				{
// 					$artistin = M("Userinfo") -> where("uid={$value['uid']}") -> find();
// 					$tagname = explode("-", $artistin['tagname']);
// 					unset($artistin['tagname']);
// 					$artistin['tagname'] = $tagname;
// 					if ($$artistin['spreadid'] != null) 
// 					{
// 						$sprice = M("Spread") -> where("spreadid = {$artistin['spreadid']}") -> find();
// 						$artistin['spreadid'] = $sprice;
// 					}
// 					$artistinfo[$key]['artistin'] = $artistin;
// 				}
// 			} 
// 			$pageSize   = 10;
// 			$start      = ($currentPage-1)*$pageSize;
// 			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
// 			$list = M('User') -> order("dtttype desc,rmtype desc") -> limit($start, $pageSize) -> select();
// 			var_dump($list);
// 		}
		/*
		 *首页艺人库/需求厅图片接口
		 *
		 *
		 */
		public  function backgroundImage()
		{
			$arbackground = M("Background") -> where("type=0") -> select();
			$artistImage = "http://192.168.1.119/loveshow/Public/Uploads/Background/".$arbackground['0']['bdimage'];
			
			$debackground = M("Background") -> where("type=1")  -> select();
			$demandImage = "http://192.168.1.119/loveshow/Public/Uploads/Background/".$debackground['0']['bdimage'];
	
			if ($arbackground != null || $debackground != null) 
			{
				$array['result'] = 0;
				$array['message'] = "图片信息获取成功！";
				$array['artistImage'] = $artistImage;
				$array['demandImage'] = $demandImage;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "图片信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
 		/*
 		 *轮播图接口
 		 *
 		 *
 		 */
		public function carouselImg()
		{
			$carouselimg = M("Carouselimg") -> select();
			
			foreach ($carouselimg as $key=>$value)
			{
				$data = array();
				if ($value['id'] == null) 
				{
					$id = "";
				}
				if ($value['url'] == null)
				{
					$url = "";					
				}
				else 
				{
					$url = $value['url'];
				}
				
				$data = array(
						'img'=>"http://192.168.1.119/loveshow/Public/Uploads/Carouselimg/".$value['cimg'],
						'type'=>$value['type'],
						'id'=>$id,
						'url'=>$url
				);
				$carouselimg[$key] = $data;
			}
			//var_dump($carouselimg);
			if ($carouselimg != null)
			{
				$array['result'] = 0;
				$array['message'] = "图片信息获取成功";
				$array['list'] = $carouselimg;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "图片信息获取失败";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
 		/*
 		 *机构列表
		 *
 		 *
 		 */
		public function institutionInfo($page=null, $type=null)
		{
			$type = isset($type) ? $type:1;
			$currentpage = isset($page) ? $page:1;
			
			$pagesize    = 10;
				
			$pagecount = ceil((M("Otion") ->count("id"))/$pagesize);
			//var_dump($pagecount);
			if ($pagecount < $page)
			{
				$array['result'] = -2;
				$array['message'] = "没有更多的数据了！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
				
			$start       = ($currentpage-1)*$pagesize;// 分页显示输出
					
			$condition = array(
					'type'=>$type
			);
			$institutionInfo = M("Otion") -> where($condition)-> limit($start, $pagesize) -> select();
			//var_dump($institutionInfo);
			foreach ($institutionInfo as $key=>$value)
			{
				$data = array();
				$data = array(
						'id'=>$value['id'],
						'logo'=>"http://odgzite5t.bkt.clouddn.com/".$value['poster']."-inform",
						'type'=>$value['type']
				);
				$institutionInfo[$key] = $data;
			}
			if ($institutionInfo != null)
			{
				$array['result'] = 0;
				$array['message'] = "机构信息获取成功！";
				$array['list'] = $institutionInfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "机构信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		/*
		 *通告列表
		 *
		 *
		 */
		public function announceInfo($page=null)
		{
			$currentpage = isset($page) ? $page:1;  

			$pagesize    = 10;
			
			$pagecount = ceil((M("Annunciate") ->count("id"))/$pagesize);
			//var_dump($pagecount);
			if ($pagecount < $page)
			{
				$array['result'] = -2;
				$array['message'] = "没有更多的数据了！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			
			$start       = ($currentpage-1)*$pagesize;// 分页显示输出

			$announceInfo = M("Annunciate") -> limit($start , $pagesize) -> select();
			foreach ($announceInfo as $key=>$value)
			{
				$data = array();
				$dateandtimes = date("Y-m-d",strtotime($value['time']));
				$data = array(
						'aid' =>$value['id'],
						'title'=>$value['annunciate_r'],
						'picture'=>"http://192.168.1.119/loveshow/Public/Uploads/Images/".$value['annunciate_p'],
						'content'=>$value['annunciate_w'],
						'time'=>$dateandtimes
				);
				$announceInfo[$key] = $data;
			}
			
			if ($announceInfo != null)
			{
				$array['result'] = 0;
				$array['message'] = "通告信息获取成功！";
				$array['list'] = $announceInfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "通告信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
 		/*
 		 *才艺访
 		 *
 		 *
 		 */
		 public function mirrorInfo($page=null)
		{
			$currentpage = isset($page) ? $page:1;
			
			$pagesize    = 3;
			$pagecount = ceil((M("Userinfo") -> where("tstype=1") ->count("id"))/$pagesize);
			//var_dump($pagecount);
			if ($pagecount < $page)
			{
				$array['result'] = -2;
				$array['message'] = "没有更多的数据了！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			$start = ($currentpage-1)*$pagesize;// 分页显示输出
			
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$mirrorInfo = M('Userinfo') -> where("tstype=1") -> limit($start , $pagesize) -> select();
			 
			/* $mirrorInfo = M("Userinfo") -> select(); */
			//var_dump($institutionInfo);
			 foreach ($mirrorInfo as $key=>$value)
			{
				$data = array();
				if ($value['background'] == null) 
				{
					$photoInfo = explode(",", $value["photo"]);
					$data = array(
							"uid" =>$value["uid"],
							"mirror"=>$photoInfo[0]
					);
				}
				else
				{
					$data = array(
							'uid'=>$value['uid'],
							'mirror'=>$value['background']
					);
				}
				$mirrorInfo[$key] = $data;
			}
			if ($mirrorInfo != null)
			{
				$array['result'] = 0;
				$array['message'] = "图片信息获取成功！";
				$array['list'] = $mirrorInfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "图片信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
 		} 
		/*
		 *机构分类接口
		 *
		 *
		 */
		public function otionInfo()
		{
			$otioninfo = M("Otype") -> select();
			
			foreach ($otioninfo as $key=>$value)
			{
				$data = array();
				$data = array(
						'type'=>$value['tid'],
						'typename'=>$value['tname'],
						'typepic'=>"http://192.168.1.119/loveshow/Public/Uploads/Otiontypeimg/".$value['tpic']
				);
				$otioninfo[$key] = $data;
			}
			
			if ($otioninfo != null)
			{
				$array['result'] = 0;
				$array['message'] = "分类信息获取成功！";
				$array['list'] = $otioninfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "分类信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		/*
		 *秀专访
		 *
		 *
		 */
		public function showZhuanFang($page=null)
		{
			$currentpage = isset($page) ? $page:1;
			
			$pagesize    = 3;
			$pagecount = ceil((M("Showzf") ->count("sid"))/$pagesize);
			if ($pagecount < $page)
			{
				$array['result'] = -2;
				$array['message'] = "没有更多的数据了！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			$start       = ($currentpage-1)*$pagesize;// 分页显示输出
			
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$showzfInfo = M('Showzf') -> limit($start , $pagesize) -> select();
			foreach ($showzfInfo as $key=>$value)
			{
				$data = array();
				$date = date("Y-m-d",strtotime($value['dateandtime']));
				$v=explode('-',$date);
				$dateandtimes = $v[1]."-".$v[2];
				
				$data = array(
						'title'=>$value['title'], 
						'poster'=>"http://192.168.1.119/loveshow/Public/Uploads/Showzf/".$value['poster'],
						'url'=>"www.baidu.com",
						"dateandtime"=>$dateandtimes,
						'clicktimes'=>$value['clicktimes']
				);
				$showzfInfo[$key] = $data;
				
			}
			//var_dump($showzfInfo);
			if ($showzfInfo != null)
			{
				$array['result'] = 0;
				$array['message'] = "专访信息获取成功！";
				$array['list'] = $showzfInfo;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "专访信息获取失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		
	}